package com.huiyeji.dynamic.core.provider;

import com.huiyeji.dynamic.core.DynamicContext;
import com.huiyeji.dynamic.core.DynamicStatement;

import java.util.Arrays;

public class IntegerCalculationParamProvider extends AbstractParamProvider {
    public IntegerCalculationParamProvider(String paramName) {
        super(paramName);
    }

    @Override
    public Object get(DynamicContext context, String paramName) {
        String splitString = "";
        String methodName = "";
        if (paramName.contains("+")){
            splitString = "\\+";
            methodName = "add";
        }else if (paramName.contains("*")){
            splitString = "\\*";
            methodName = "mul";
        } else if (paramName.contains("/")){
            splitString = "/";
            methodName = "div";
        } else if (paramName.contains("-")){
            splitString = "-";
            methodName = "sub";
        } else if (paramName.contains("==")){
            splitString = "==";
            methodName = "eq";
        }else if (paramName.contains(">=")){
            splitString = ">=";
            methodName = "gte";
        } else if (paramName.contains("<=")){
            splitString = "<=";
            methodName = "lte";
        }else if (paramName.contains(">")){
            splitString = ">";
            methodName = "gt";
        } else if (paramName.contains("<")){
            splitString = "<";
            methodName = "lt";
        } else if (paramName.contains("!=")){
            splitString = "!=";
            methodName = "ne";
        } else {
            throw new RuntimeException("parse error !");
        }
        String[] paramArray = paramName.split(splitString);
        String param1 = paramArray[0].trim();
        String param2 = paramArray[1].trim();
        ParamProvider paramProvider1 = context.getParamProviderBuilderMatcher().matchObject(param1).build(param1);
        ParamProvider paramProvider2 = context.getParamProviderBuilderMatcher().matchObject(param2).build(param2);
        return context.getExecutor().execute(context,
                new DynamicStatement(
                        paramName,
                        true,
                        null,
                        null,
                        "integer",
                        methodName,
                        null,
                        Arrays.asList(paramProvider1,paramProvider2)
                )
        );
    }
}
